<?php
/**
 * AdminModel.class.php     系统管理员模型
 *
 * @author                  mashanling(msl-138@163.com)
 * @date                    2012-03-21 14:26:32
 * @last modify             2012-09-26 11:01:19 by mashanling
 */

!defined('ROOT') && exit('Access Denied');

class AdminModel extends CommonModel {
    protected $auto = array(//自动填充
        'add_time'        => 'gmtime#insert',//添加时间
        'password'        => 'setPassword#data',//密码
        'is_restrict'     => 'getCheckboxValue',//是否绑定登陆
        'lock_start_time' => 'strtotime',//锁定开始时间 by mashanling on 2012-09-05 15:56:05
        'lock_end_time'   => 'strtotime',//锁定结束时间 by mashanling on 2012-09-05 15:56:25
    );
    /**
     * 数据表字段信息
     * filter: 数据类型，array(数据类型(string,int,float...),Filter::方法参数1,参数2...)
     * validate: 自动验证，支持多个验证规则
     *
     * @see Model.class.php create()方法对数据过滤
     * @see CommonModel.class.php __construct()方法设置自动验证字段_validate
     */
    protected $db_fields = array (
        'admin_id'          => array('filter' => 'int', 'validate' => 'unsigned#PRIMARY_KEY,DATA,INVALID'),//自增主键

        //所属角色id
        'role_id'           => array('filter' => 'int', 'validate' => 'unsigned#PLEASE_SELECT,BELONG_TO_ROLE#0}'),

        //用户名
        'username'          => array('validate' => array('checkUsername#PLEASE_ENTER,USERNAME#data', 'checkLength#USERNAME#value|0|15')),

        //密码 AdminAction::edit调到 by mashanling on 2012-07-13 17:44:10
        'password'          => array('validate' => array('checkPassword#PLEASE_ENTER,PASSWORD#data', 'checkLength#PASSWORD#value|6|16')),

        //真实姓名 ActminAction::edit调到 by mashanling on 2012-07-13 17:45:12
        'realname'          => array('validate' => array('notblank#REALNAME', 'checkLength#USERNAME#value|0|24')),
        'add_time'          => array('filter' => 'int'),//添加时间
        'last_login_time'   => array('filter' => 'int'),//最后登陆时间
        'last_login_ip'     => array('filter' => 'ip'),//最后登陆ip
        'login_num'         => array('filter' => 'int'),//登陆次数
        'is_restrict'       => array('filter' => 'int'),//是否绑定登陆
        'mac_address'       => null,//网卡信息
        'lock_start_time'   => array('validate' => 'checkLength#LOCK,START,TIME#value|19|null'),//锁定开始时间
        'lock_end_time'     => array('validate' => 'checkLength#LOCK,END,TIME#value|19|null'),//锁定结束时间
        'lock_memo'         => array('validate' => array('return#LOCK,MEMO', 'checkLength#LOCK,MEMO#value|0|60')),//锁定备注

        //确认密码，_开头，不会入库
        '_password_confirm' => array('validate' => array('checkPassword#PLEASE_ENTER,CONFIRM_PASSWORD#data|1', 'checkLength#CONFIRM_PASSWORD#value|6|16')),
        '_old_password'     => null,//原密码，修改密码是用到 by mashanling on 2012-07-13 17:55:11
    );
    protected $pk = 'admin_id';//主键
    protected $true_table_name = TB_ADMIN;//表

    /**
     * 验证原密码是否正确
     *
     * @author       mashanling(msl-138@163.com)
     * @date         2012-07-14 10:40:46
     * @last modify  2012-07-14 10:40:46 by mashanling
     *
     * @param string $password 密码
     *
     * @return mixed 验证通过，返回true，否则返回错误信息
     */
    function checkOldPassword($password) {

        if ($password === '') {
            return false;
        }

        return md5($this->encryptPassword($password)) == $this->module->admin_info['password'] ? true : L('CN_YUAN,PASSWORD,NOT_CORRECT');
    }

    /**
     * 确认密码
     *
     * @param string $password 密码
     * @param array  $data     _POST数据
     * @param bool   $confirm  是否确认密码
     *
     * @return mixed 如果验证通过，返回true，否则，如果新增，确认密码为空，返回输入确认密码，否则，返回false
     */
    protected function checkPassword($password, $data, $confirm = false) {
        $password           = trim($password);
        $admin_id           = isset($data[$this->pk]) ? $data[$this->pk] : 0;
        $original_password  = $data['password'];

        if ($admin_id && $confirm) {//编辑，确认密码，如果密码为空，返回true，否则判断两次输入的密码是否一致
            return $password == '' || $password == $original_password ? true : L('PASSWORD_NOT_SAME');
        }
        elseif (!$admin_id && $password == '') {//新增
            return false;
        }
        elseif (!$admin_id && $confirm && $password != $original_password) {//新增，确认密码
            return L('PASSWORD_NOT_SAME');
        }

        return true;
    }

    /**
     * 验证用户是否已经存在
     *
     * @param string $username 用户名
     * @param array  $data     _POST数据
     *
     * @return mixed 验证成功，返回true。否则，如果未输入，返回提示信息，否则返回false
     */
    protected function checkUsername($username, $data) {

        if ($username === '') {//如果未输入，提示输入
            return false;
        }

        $users    = $this->getCache();
        $admin_id = isset($data[$this->pk]) ? $data[$this->pk] : 0;

        foreach ($users as $user_id => $user) {

            if (strcasecmp($username, $user['username']) == 0 && $admin_id != $user_id) {
                return L('USERNAME,EXIST');
            }
        }

        return true;
    }

    /**
     * 自动填空密码，则不需要修改密码
     *
     * @param string $password 密码
     * @param array  $data     _POST数据
     *
     * @return mixed 如果确定密码为空，返回false，否则返回经md5加密后的密码
     */
    protected function setPassword($password, $data) {
       return $data['_password_confirm'] === '' ? false : $this->encryptPassword($password);
    }
}